<template>
  <body id="paper">
  <Header></Header>
  <el-form :model="loginForm" :rules="rules" class="login-container" label-position="left"
           label-width="0px" v-loading="loading">
    <h3 class="login_title">用户登录</h3>
    <el-form-item prop="acc">
      <el-input type="text" v-model="loginForm.acc"
                auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="pw">
      <el-input type="password" v-model="loginForm.pw"
                auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-checkbox class="login_remember" v-model="checked"
                 label-position="left"><span style="color: #9495c5">记住密码</span></el-checkbox>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 40%;background: #9495c5;border: none" v-on:click="login">登录</el-button>
      <router-link to="register">
        <el-button type="primary" style="width: 40%;background: #9495c5;border: none">注册</el-button>
      </router-link>
    </el-form-item>
  </el-form>
  </body>
</template>

<script>
import Header from './common/Header'

export default {
  components: {Header},
  data () {
    return {
      rules: {
        acc: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
        pw: [{required: true, message: '密码不能为空', trigger: 'blur'}]
      },
      checked: true,
      loginForm: {
        acc: '',
        pw: ''
      },
      loading: false
    }
  },
  methods: {
    login () {
      if (this.loginForm.acc === '' || this.loginForm.pw === '') {
        this.$alert('请填写完整信息', '提示', {
          confirmButtonText: '确定'
        })
      } else {
        var _this = this
        this.$axios.post('/login', {
          acc: this.loginForm.acc,
          pw: this.loginForm.pw
        })
          .then(resp => {
            if (resp.data.code === 200) {
              // 存储登录状态
              var userType = resp.data.result
              var acc = this.loginForm.acc
              _this.$store.commit('login', acc)
              if (userType === '30030') {
                this.$router.replace('/student')
              } else {
                this.$router.replace('/teacher')
              }
              if (userType === '10010') {
                this.$router.replace('/admin')
              }
              // var path = _this.$route.query.redirect
              // _this.$router.replace({path: path === '/' || path === undefined ? '/admin/dashboard' : path})
            } else {
              this.$alert(resp.data.msg, '提示', {
                confirmButtonText: '确定'
              })
            }
          }).catch(failResponse => {
            alert('服务器响应请求失败！')
          })
      }
    }
  }
}
</script>

<style scoped>
#paper {
  background: #696aad url("../assets/img/bg/bg1.png") no-repeat 20% 40%;
  background-size: 20%;
  height: 100%;
  width: 100%;
  position: fixed;
}

body {
  margin: 0;
}

.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  position: fixed;
  top: 20%;
  right: 20%;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #9495c5;
}

.login_remember {
  margin: 0px 0px 35px 0px;
  text-align: left;
}
</style>
